package com.tp.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;

/**
 * 微信工具类
 * 
 * @author TP
 *
 */
public class WeixinUtil {
	// 全局常量
	private static String appid = "wx897ec78269967414";
	private static String secret = "";
	private static String access_token = "";
	private static long timestamp = 1460602940;

	// 获取微信接口调用凭证
	public static String getToken() {
		try {
			// 获取当前时间戳
			long now = DateUtils.getCurrentUnixTime();
			// 获取access_token
			String accesstoken = access_token;
			if ((now - timestamp) > 7000 || "".equals(accesstoken) || accesstoken == null) {
				String str = HttpKit.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
						+ appid + "&secret=" + secret);
				JSONObject json = JSON.parseObject(str);
				System.out.println(str);
				if (StrKit.notBlank(json.getString("access_token"))) {
					accesstoken = json.getString("access_token");
					access_token = accesstoken;
					timestamp = now;
				} else {
					access_token = "";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return access_token;
	}
	
	// 远程调用微信token
	public static String getWinxinToken(){
		try {
			String urls = "http://www.jkwin.com.cn/ystcomservice/wx.do?method=getAccessToken&orgId=503";
			String str = HttpKit.get(urls);
			if(StrKit.notBlank(str)){
				return str;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
